package com.tms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tms.domain.Type;
import com.tms.mapper.develop.TypeMapper;
import com.tms.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ozj
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Override
    @Cacheable(cacheNames = "types", key = "#id", unless = "#result == null")
    public Type findById(int id) {
        return typeMapper.selectByPrimaryKey(id);
    }

    @Override
    @Cacheable(cacheNames = "types", key = "#name", unless = "#result == null")
    public Type findByName(String name) {
        return typeMapper.selectByName(name);
    }

    @Override
    @Cacheable(value = "typeList", key = "#page + #pageSize", unless = "#result == null")
    public PageInfo<Type> findAllWithPaging(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Type> list = typeMapper.selectAlls();
        return new PageInfo<>(list);
    }

    @Override
    @Cacheable(value = "typeAlls", key = "'findAlls'", unless = "#result == null")
    public List<Type> findAlls() {
        return typeMapper.selectAlls();
    }

    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "types", key = "#result.id", unless = "#result == null || #result.id == 0"),
                    @CachePut(cacheNames = "types", key = "#result.name", unless = "#result == null || #result.id == 0")
            },
            evict = {
                    @CacheEvict(cacheNames = "typeAlls", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "typeList", allEntries = true, beforeInvocation = true)
            }
    )
    public Type addType(Type type) {
        typeMapper.insert(type);
        return type;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "types", key = "#type.id"),
                    @CacheEvict(cacheNames = "types", key = "#type.name"),
                    @CacheEvict(cacheNames = "typeAlls", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "typeList", allEntries = true, beforeInvocation = true)
            }
    )
    public Type deletionType(Type type) {
        typeMapper.deleteByPrimaryKey(type.getId());
        return type;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "types", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "typeAlls", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "typeList", allEntries = true, beforeInvocation = true)
            }
    )
    public void updateById(Type type) {
        typeMapper.updateByPrimaryKey(type);
    }
}
